// stores/counter.js
import { defineStore } from 'pinia'
import axios from 'axios'
export const Fenlei = defineStore('fenlei', {
    state: () => {
        return {
            FenleiList: []
        }
    },

    actions: {
        FENLEI() {
            axios.get('https://api-hmugo-web.itheima.net/api/public/v1/categories').then(res => {
                this.FenleiList = res.data.message
            })
        },
    },
})
export const Cart = defineStore('cart', {
    state: () => {
        return {
            CartiList: []
        }
    },
 persist: true,
    actions: {
        ADD_cart(item) {
            const goods_item=this.CartiList.find(res=>res.goods_id==item.goods_id)
            if(!goods_item){
                this.CartiList.push(item)
            }else{
                item.count+=1
            }
        },
        JIA(item){
            item.count+=1
        },
        JIAN(item){
            item.count-=1
        },
        Shanchu(item,index){
            this.CartiList.splice(index,1)
        },
        Fanxuan(checked){
            this.CartiList.forEach(item=>{
                item.check=checked
            })
        }
    },
    getters:{
        Allcount(){
            return this.CartiList.reduce((aa,bb)=>{
                if(bb.check){
                    return aa+bb.count
                }else{
                    return aa
                }
            },0)
        },
        Allprice(){
            return this.CartiList.reduce((aa,bb)=>{
                if(bb.check){
                    return aa+bb.count*bb.goods_price*100
                }else{
                    return aa
                }
            },0)
        },
    }
})